package com.yan.club.apply.service.impl;

import com.yan.club.apply.dto.ClubApplyDto;
import com.yan.club.apply.mapper.ClubApplyMapper;
import com.yan.club.apply.model.ClubApply;
import com.yan.club.apply.model.ClubApplyExample;
import com.yan.club.apply.service.ClubApplyService;
import com.yan.club.auth.model.AuthUser;
import com.yan.club.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.yan.club.auth.utils.SecurityUtil.getUser;
import static com.yan.club.utils.SnowflakeIdWorker.generateId;


/**
 * @author 彦
 * @since 2024/1/9 14:53
 */
@Service
public class ClubApplyServiceImpl implements ClubApplyService {

    @Resource
    private ClubApplyMapper clubApplyMapper;

    @Override
    public List<ClubApply> list(ClubApplyDto clubApplyDto) {

        ClubApplyExample example = new ClubApplyExample();
        ClubApplyExample.Criteria criteria = example.createCriteria();


        if (!StringUtils.isEmpty(clubApplyDto.getStudentNo())) {
            criteria.andStudentNoEqualTo(clubApplyDto.getStudentNo());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getStudentName())) {
            criteria.andStudentNameEqualTo(clubApplyDto.getStudentName());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getPhone())) {
            criteria.andPhoneEqualTo(clubApplyDto.getPhone());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getReason())) {
            criteria.andReasonLike("%" + clubApplyDto.getReason() + "%");
        }

        List<ClubApply> clubApplies = clubApplyMapper.selectByExample(example);

        return clubApplies;
    }

    @Override
    public Result<String> delete(Long applyId) {
        clubApplyMapper.deleteByPrimaryKey(applyId);
        return Result.success();
    }


    /**
     * 新增
     *
     * @param clubApply
     * @return
     */
    @Override
    public Result<String> save(ClubApply clubApply) {

        Long applyId = generateId();

        clubApply.setId(applyId);
        clubApply.setCreateTime(new Date());
        clubApply.setUpdateTime(new Date());

        AuthUser authUser = getUser();
        clubApply.setPhone(authUser.getPhonenumber());
        clubApply.setStudentNo(authUser.getUserName());
        clubApply.setStudentName(authUser.getName());
        clubApply.setStatus(0);
        clubApplyMapper.insert(clubApply);


        return Result.success();
    }

    /**
     * 修改状态 1通过 2不通过
     *
     * @param applyId
     * @return
     */
    @Override
    public Result<String> updateStatus(Long applyId, int status) {

        ClubApply clubApply = clubApplyMapper.selectByPrimaryKey(applyId);

        clubApply.setStatus(status);
        clubApplyMapper.updateByPrimaryKey(clubApply);

        return Result.success();
    }


    @Override
    public List<ClubApply> getApplyByStudentNo(ClubApplyDto clubApplyDto, String studentNo) {

        ClubApplyExample example = new ClubApplyExample();
        ClubApplyExample.Criteria criteria = example.createCriteria();

        criteria.andStudentNoEqualTo(studentNo);

        if (!StringUtils.isEmpty(clubApplyDto.getStudentNo())) {
            criteria.andStudentNoEqualTo(clubApplyDto.getStudentNo());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getStudentName())) {
            criteria.andStudentNameEqualTo(clubApplyDto.getStudentName());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getPhone())) {
            criteria.andPhoneEqualTo(clubApplyDto.getPhone());
        }

        if (!StringUtils.isEmpty(clubApplyDto.getReason())) {
            criteria.andReasonLike("%" + clubApplyDto.getReason() + "%");
        }

        List<ClubApply> clubApplies = clubApplyMapper.selectByExample(example);

        return clubApplies;

    }

}
